<template>
  <el-container v-loading="loading">

    <el-row>
      <el-button
        icon="fa fa-fw fa-refresh"
        size="mini"
        type="info"
        @click="handleRefresh">
      </el-button>

      <el-button
        icon="fa fa-fw fa-check"
        size="mini"
        type="success"
        @click="handleApplyAccept"> 同意申请
      </el-button>
    </el-row>

    <el-table
      @selection-change="handleSelectionChange"
      :data="tableData"
      tooltip-effect="dark"
      border
      :default-sort="{prop: 'date', order: 'descending'}"
      :row-class-name="tableRowClassName"
      size="small">
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>

      <el-table-column
        prop="name"
        width="120"
        label="姓名">
      </el-table-column>

      <el-table-column
        prop="user.nickname"
        width="120"
        label="昵称">
      </el-table-column>

      <el-table-column
        prop="user.sex"
        label="性别"
        width="80"
        sortable>
        <template slot-scope="scope">
          <el-tag v-if="scope.row.user.sex==1">男</el-tag>
          <el-tag v-if="scope.row.user.sex==2">女</el-tag>
        </template>
      </el-table-column>


      <el-table-column
        prop="user.phone"
        label="电话"
        width="120"
        sortable>
      </el-table-column>

      <el-table-column
        prop="type"
        label="类型"
        width="80"
        sortable>
        <template slot-scope="scope">
          <el-tag v-if="scope.row.type==1">跑腿</el-tag>
          <el-tag v-if="scope.row.type==2">代驾</el-tag>
          <el-tag v-if="scope.row.type==3">家政</el-tag>
        </template>
      </el-table-column>

      <el-table-column
        prop="identity"
        label="身份证"
        sortable>
      </el-table-column>

      <el-table-column
        prop="card_front"
        label="身份证正面"
        width="100"
        show-overflow-tooltip>
        <template slot-scope="scope">
          <img :src="scope.row.card_front" class="logo-img">
        </template>
      </el-table-column>

      <el-table-column
        prop="card_back"
        label="身份证反面"
        width="100"
        show-overflow-tooltip>
        <template slot-scope="scope">
          <img :src="scope.row.card_back" class="logo-img">
        </template>
      </el-table-column>

      <el-table-column
        prop="license_front"
        label="驾照"
        width="100"
        show-overflow-tooltip>
        <template slot-scope="scope">
          <img :src="scope.row.license_front" class="logo-img">
        </template>
      </el-table-column>

      <el-table-column
        prop="health_front"
        label="健康证"
        width="100"
        show-overflow-tooltip>
        <template slot-scope="scope">
          <img :src="scope.row.health_front" class="logo-img">
        </template>
      </el-table-column>

      <el-table-column
        prop="create_time"
        label="申请时间"
        sortable>
        <template slot-scope="scope">
          <i class="fa fa-clock-o fa-fw"></i>
          <span style="margin-left: 10px">{{ scope.row.create_time }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            circle
            icon="fa fa-fw fa-eye"
            size="mini"
            type="info"
            title="查看详情"
            @click="handleDataView(scope.$index, scope.row)">
          </el-button>

          <el-button
            circle
            icon="fa fa-fw fa-check"
            size="mini"
            type="success"
            title="同意"
            @click="handleApplyAcceptItem(scope.$index, scope.row)">
          </el-button>

          <el-button
            circle
            icon="fa fa-fw fa-remove"
            size="mini"
            type="danger"
            title="拒绝"
            @click="handleApplyReject(scope.$index, scope.row)">
          </el-button>

        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pagination.currentPage"
      :page-sizes="pagination.pageSizes"
      :page-size="pagination.pageSize"
      :total="pagination.total"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>

    <!-- 提示 -->
    <el-dialog
      title="数据详情"
      :visible.sync="dialogVisible"
      width="30%">
      <div v-if="currentDataIndex != null">
        <!--
        create_time:'',                //类型：String  必有字段  备注：申请时间
        sex:'',                //类型：Number  必有字段  备注：无-->

        <p><span class="label">姓名：</span>{{tableData[currentDataIndex].name}}</p>
        <p><span class="label">昵称：</span>{{tableData[currentDataIndex].user.nickname}}</p>
        <p><span class="label">性别：</span>
          <el-tag v-if="tableData[currentDataIndex].user.sex==1">男</el-tag>
          <el-tag v-if="tableData[currentDataIndex].user.sex==2">女</el-tag>
        </p>
        <p><span class="label">电话：</span>{{tableData[currentDataIndex].user.phone}}</p>
        <p><span class="label">身份证：</span>{{tableData[currentDataIndex].identity}}</p>
        <p><span class="label">类型：</span>
          <el-tag v-if="tableData[currentDataIndex].type==1">跑腿</el-tag>
          <el-tag v-if="tableData[currentDataIndex].type==2">代驾</el-tag>
          <el-tag v-if="tableData[currentDataIndex].type==3">家政</el-tag>
        </p>
        <p>
          <span class="label">身份证正面：</span>
          <img :src="tableData[currentDataIndex].card_front" class="big-img">
        </p>
        <p>
          <span class="label">身份证反面：</span>
          <img :src="tableData[currentDataIndex].card_back" class="big-img">
        </p>
        <p>
          <span class="label">驾照：</span>
          <img :src="tableData[currentDataIndex].license_front" class="big-img">
        </p>
        <p>
          <span class="label">健康证：</span>
          <img :src="tableData[currentDataIndex].health_front" class="big-img">
        </p>
        <p><span class="label">申请时间：</span>{{tableData[currentDataIndex].create_time}}</p>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">关 闭</el-button>
      </span>
    </el-dialog>

  </el-container>
</template>

<script>
  export default {
    mounted: function () {
      this.getData();
    },
    methods: {
      /**
       * 数据请求
       * */
      getData: function () {
        let _this = this;
        this.api.getRunnerApplyTabulation({
          page: this.pagination.currentPage,
          paginate: this.pagination.pageSize
        }).then(function (res) {
          if (res.code == 1) {
            _this.tableData = res.data.data;
            _this.pagination.total = res.data.total;
          }
          _this.loading = false;
        }).catch(function (res) {
          throw new Error(res);
        });
      },
      /**
       * 分页数改变
       */
      handleSizeChange: function (pageSize) {
        this.pagination.pageSize = pageSize;
        this.getData();
      },
      /**
       * 当前页面改变
       * @param currentPage
       */
      handleCurrentChange: function (currentPage) {
        this.pagination.currentPage = currentPage;
        this.getData();
      },
      tableRowClassName({row, rowIndex}) {
        if (rowIndex === 1) {
          return 'warning-row'
        } else if (rowIndex === 3) {
          return 'success-row'
        }
        return ''
      },
      /**
       * 同意申请
       * @param index
       * @param row
       */
      applyAccept({id: id}) {
        let _this = this;
        _this.api.runnerApplyAccept({id: id}).then(function (res) {
          if (res.code == 1) {
            _this.getData();
            _this.$message({
              showClose: true,
              message: res.msg,
              type: 'success'
            });
          } else {
            _this.$message({
              showClose: true,
              message: res.msg,
              type: 'warning'
            });
          }
        }).catch(function (error) {
          throw new Error(error);
        });
      },
      /**
       * 数据刷新
       */
      handleRefresh: function () {
        this.getData();
        this.$message({
          showClose: true,
          message: '刷新成功',
          type: 'success'
        });
      },
      /**
       * 批量同意启用
       */
      handleApplyAccept: function () {
        let _this = this;
        let arr = [];
        if (this.multipleSelection.length <= 0) {
          _this.$message({
            showClose: true,
            message: '暂无选中',
            type: 'warning'
          });
        }
        for (var i = 0; i < this.multipleSelection.length; i++) {
          arr.push(this.multipleSelection[i].id);
        }
        this.$confirm('此操作将同意该申请, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          _this.applyAccept({id: arr.join(',')});
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消该操作'
          })
        })
      },
      /**
       * 同意申请
       */
      handleApplyAcceptItem(index, row) {
        let _this = this;
        let _id = row.id;
        this.$confirm('此操作将同意该申请, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          _this.applyAccept({id: _id});
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消该操作'
          })
        })
      },
      /**
       * 拒绝申请
       * @param index
       * @param row
       */
      handleApplyReject(index, row) {
        let _this = this;
        let _id = row.id;
        this.$prompt('请输入拒绝原因', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
        }).then(({value}) => {
          _this.api.runnerApplyReject({id: _id, remarks: value}).then(function (res) {
            if (res.code == 1) {
              _this.getData();
              _this.$message({
                showClose: true,
                message: res.msg,
                type: 'success'
              });
            } else {
              _this.$message({
                showClose: true,
                message: res.msg,
                type: 'warning'
              });
            }
          }).catch(function (error) {
            throw new Error(error);
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '取消操作'
          });
        });
      },
      /**
       * 数据视图
       * */
      handleDataView: function (index, row) {
        this.currentDataIndex = index;
        this.dialogVisible = true;
      },
      /**
       * 表格选中状态记录
       * @param val
       */
      handleSelectionChange(val) {
        this.multipleSelection = val;
      }
    },
    data() {
      return {
        loading: true,  // 页面加载
        tableData: [
          {
            id: '',                //类型：Number  必有字段  备注：无
            name: '',                //类型：String  必有字段  备注：姓名
            uid: '',                //类型：Number  必有字段  备注：关联用户
            identity: '',                //类型：String  必有字段  备注：身份证
            card_front: '',                //类型：String  必有字段  备注：身份证正面
            card_back: '',                //类型：String  必有字段  备注：反面
            type: '',                //类型：Number  必有字段  备注：1，跑腿；2，代驾；3，家政
            create_time: '',                //类型：String  必有字段  备注：申请时间
            money: '',                //类型：String  必有字段  备注：余额
            license_front: '',                //类型：String  必有字段  备注：驾照正面
            license_back: '',                //类型：String  必有字段  备注：驾照反面
            health_front: '',                //类型：String  必有字段  备注：健康证正面
            user: {                //类型：Object  必有字段  备注：无
              id: '',                //类型：Number  必有字段  备注：无
              nickname: '',                //类型：String  必有字段  备注：无
              sex: '',                //类型：Number  必有字段  备注：无
              phone: ''                //类型：String  必有字段  备注：无
            }
          }
        ],
        multipleSelection: [],
        pagination: {
          total: 5,
          currentPage: 1,
          pageSize: 10,
          pageSizes: [5, 10, 20, 25, 30, 50, 100],
        },
        dialogVisible: false,   // 弹框
        currentDataIndex: null,
      }
    },
  }
</script>

<style scoped>
  .el-container {
    display: block;
  }

  .el-table, .el-row {
    width: 98% !important;
    margin: 15px auto;
  }

  .el-pagination {
    text-align: center;
    margin: 15px 0;
  }

  .el-table .success-row {
    background: #f0f9eb;
  }

  .logo-img {
    width: 40px;
    height: 40px;
  }

  .label {
    width: 100px;
    display: inline-block;
    text-align: right;
    margin-right: 15px;
  }

  .big-img {
    width: 400px;
    height: 200px;
  }
</style>
